/*
 *  Copyright 2013 National Institute of Advanced Industrial Science and Technology
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.sss.server;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.sss.common.DataReceiver;
import org.sss.mapreduce.SssException;
import org.sss.mapreduce.StorageNode;

public class ServerUtil {
  @SuppressWarnings("unused")
  private static Logger logger = SssServer.getLogger(ServerUtil.class);

  public static Map<StorageNode, Long> convertLongArrayToStorageNodeKeyMap(List<StorageNode> ns, long[] vs) throws SssException {
    if (ns.size() != vs.length) {
      throw new SssException("Mismatch size.");
    }
    Map<StorageNode, Long> m = new HashMap<StorageNode, Long>();
    for (int i = 0;i < vs.length;++i) {
      m.put(ns.get(i), vs[i]);
    }
    return m;
  }

  public static <T> DataReceiver<List<T>> listDataReceiver(final DataReceiver<T> src) {
    return new DataReceiver<List<T>>() {
      public void put(List<T> data) throws SssException {
        for (T d: data) {
          src.put(d);
        }
      }
      public void close() throws SssException {
        src.close();
      }
      public void setError(Throwable e) {
        src.setError(e);
      }
      public Throwable getError() {
        return getError();
      }
    };
  }
}
